package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.common.ResultInfo;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.ApplyService;
import com.woniu.adopt.vo.EchartsVo;
import com.woniu.adopt.vo.ApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 志愿者申请controller
 */
@RestController
@RequestMapping("apply")
@Api(value = "志愿者申请申请接口", description = "志愿者申请申请接口")
public class ApplyController implements ResultInfo {
    @Autowired
    ApplyService applyService;

    @GetMapping("/applyList")
    @ApiOperation(value = "显示志愿者申请列表，包括搜索与分页功能")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageNum", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "每页显示多少条", required = true, defaultValue = "3"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "用户名称", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "date", name = "time", value = "申请时间", required = false),
    })
    public ResultResponse<PageInfo<ApplyVo>> petAdoptList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
                                                             String name, Date time) {
        ResultResponse<PageInfo<ApplyVo>> result = null;
        try {
            PageInfo<ApplyVo> petAdoptPageInfo = applyService.queryPetAdoptByPage(pageNum, pageSize, name, time);
            result = new ResultResponse<>(SUCCESS_CODE, SUCCESS_INFO, petAdoptPageInfo);
        } catch (RuntimeException e) {
            result = new ResultResponse<>(FAILURE_CODE, FAILURE_INFO);
            e.printStackTrace();
        }
        return result;
    }

    @PutMapping("/changStatus/{id}/{status}")
    @ApiOperation(value = "修改志愿者申请申请表的审核状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "志愿者申请申请表id", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "status", value = "志愿者申请申请状态", required = true),})
    public ResultResponse<Void> changStatus(@PathVariable Integer id, @PathVariable Integer status) {
        ResultResponse<Void> result = null;
        try {
            applyService.modify(id, status, new Date());
            result = new ResultResponse<>(SUCCESS_CODE, SUCCESS_INFO);
        } catch (RuntimeException e) {
            result = new ResultResponse<>(FAILURE_CODE, FAILURE_INFO);
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/oldApplyList")
    @ApiOperation(value = "显示历史志愿者申请列表，包括搜索与分页功能")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageNum", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "每页显示多少条", required = true, defaultValue = "3"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "用户名称", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "date", name = "time", value = "申请时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "status", value = "审核状态", required = false),
    })
    public ResultResponse<PageInfo<ApplyVo>> oldPetAdoptList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
                                                                String name, Date time,
                                                                @RequestParam(value = "status") Integer status) {
        ResultResponse<PageInfo<ApplyVo>> result = null;
        try {
            PageInfo<ApplyVo> petAdoptPageInfo = applyService.queryOldPetAdoptByPage(pageNum, pageSize, name, time, status);
            result = new ResultResponse<>(SUCCESS_CODE, SUCCESS_INFO, petAdoptPageInfo);
        } catch (RuntimeException e) {
            result = new ResultResponse<>(FAILURE_CODE, FAILURE_INFO);
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/applyNum")
    @ApiOperation(value = "志愿者申请列表数量")
    public ResultResponse<Integer> petAdoptNum() {
        ResultResponse<Integer> result = null;
        try {
            Integer num = applyService.queryApplyByNum();
            result = new ResultResponse<>(SUCCESS_CODE, SUCCESS_INFO, num);
        } catch (RuntimeException e) {
            result = new ResultResponse<>(FAILURE_CODE, FAILURE_INFO);
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/weekNum")
    @ApiOperation(value = "志愿者申请近一周申请数量")
    public ResultResponse<List<EchartsVo>> weekNum() {
        ResultResponse<List<EchartsVo>> result = null;
        try {
            List<EchartsVo> list = applyService.queryWeekNum();
            result = new ResultResponse<>(SUCCESS_CODE, SUCCESS_INFO, list);
        } catch (RuntimeException e) {
            result = new ResultResponse<>(FAILURE_CODE, FAILURE_INFO);
            e.printStackTrace();
        }
        return result;
    }
}
